package com.bensonbasement.snorflux.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

import com.bensonbasement.snorflux.server.SnorfluxServerHandler;

public class StartServerDialog extends JDialog {
	
	private JTextField portField;
	private JCheckBox loadSavedGameCheckBox;
	private File saveFile = null;
	private JTextField filenameField;
	private JCheckBox joinGameCheckBox;
	private JLabel playerNameLabel;
	private JTextField playerNameField;
	private JButton startButton;
	private JButton cancelButton;
	
	private boolean done = false;
	
	public StartServerDialog(JFrame parent) {
		super(parent,"Start server",true);
		this.setLayout(new MigLayout());
		
		JLabel portLabel = new JLabel("Port: ");
		this.portField = new JTextField(Integer.toString(SnorfluxServerHandler.DEFAULT_PORT),5);
		
		this.loadSavedGameCheckBox = new JCheckBox("Load a saved game",false);
		loadSavedGameCheckBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					JFileChooser fileChooser = new JFileChooser();
					int result = fileChooser.showOpenDialog(StartServerDialog.this);
					if (result == JFileChooser.APPROVE_OPTION) {
						saveFile = fileChooser.getSelectedFile();
						filenameField.setText(saveFile.getName());
					}
					else {
						loadSavedGameCheckBox.setSelected(false);
					}
				}
				else {
					saveFile = null;
					filenameField.setText("");
				}
			}
		});
		filenameField = new JTextField(20);
		filenameField.setEditable(false);
		
		this.joinGameCheckBox = new JCheckBox("Join the game",true);
		joinGameCheckBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				boolean enabled = (e.getStateChange() == ItemEvent.SELECTED);
				StartServerDialog.this.playerNameLabel.setEnabled(enabled);
				StartServerDialog.this.playerNameField.setEnabled(enabled);
			}
		});
		
		this.playerNameLabel = new JLabel("Your name: ");
		this.playerNameField = new JTextField("Player",10);
		this.startButton = new JButton("Start");
		this.startButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				StartServerDialog.this.done = true;
				StartServerDialog.this.setVisible(false);
			}
			
		});
		this.cancelButton = new JButton("Cancel");
		this.cancelButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				StartServerDialog.this.done = false;
				StartServerDialog.this.setVisible(false);
			}
		});
		this.add(portLabel);
		this.add(this.portField, "wrap");
		this.add(this.loadSavedGameCheckBox,"wrap");
		this.add(this.filenameField,"wrap");
		this.add(this.joinGameCheckBox, "wrap");
		this.add(this.playerNameLabel);
		this.add(this.playerNameField, "wrap");
		this.add(this.startButton);
		this.add(this.cancelButton);
		this.pack();
	}
	
	public int getPort() {
		return Integer.parseInt(this.portField.getText());
	}
	
	public String getPlayerName() {
		return this.playerNameField.getText();
	}

	public boolean startServerRequested() {
		return done;
	}
	
	public boolean connectRequested() {
		return this.joinGameCheckBox.isSelected();
	}
	
	public File getSaveFile() {
		return saveFile;
	}
}
